// initialise plugins
jQuery(document).ready(function() {

	// Expand Panel
	jQuery('#open').click(function () {
		jQuery('#slidingpanel').click(
			function () {
				jQuery('#panel, #toggle',jQuery(this)).stop().animate({'marginLeft':'196px'},500);
			}
		);
	});
	
	jQuery('#close').click(function () {
		jQuery('#slidingpanel').click(
			function () {
				jQuery('#panel',jQuery(this)).stop().animate({'marginLeft':'0px'},500);
			}
		);
	});
	
	// Switch buttons from "Open" to "Close" on click
	jQuery("#toggle div").click(function () {
		jQuery("#toggle div").toggle();
	});	
	
	
	jQuery('#panel .stCols span:nth-child(5n+1)').addClass('nomargin');
	jQuery('#panel .stPat a:nth-child(5n+1)').addClass('nomargin');
	
	
	// Flying panel Up/Down
	var offset = jQuery("#slidingpanel").offset();
	var topPadding = 150;
	jQuery(window).scroll(function() {
		if (jQuery(window).scrollTop() > offset.top) {
			jQuery("#slidingpanel").stop().animate({
				marginTop: jQuery(window).scrollTop() - offset.top + topPadding
			});
		} else {
			jQuery("#slidingpanel").stop().animate({
				marginTop: 0
			});
		};
	});
	
	// Call jQuery ColorPicker
	jQuery('#panel #bgColor').ColorPicker({
		onChange:function(hsb, hex, rgb){
			jQuery('#panel').find('#bgColor').css({backgroundColor:'#' + hex});
			var bodybg = jQuery('body').css('background-image');
			
			jQuery('body').css({backgroundColor:'#' + hex});
		},
		onSubmit:function(hsb, hex, rgb, el){
			jQuery('#panel .stCols span').removeClass('current');
			jQuery(el).find('#bgColor').css({backgroundColor:'#' + hex});
			jQuery(el).ColorPickerHide();
		}
	});
	
	// Set Menu Color
	jQuery('a.styleswitch').bind('click',function() {
		var $this = $(this);
		var mPath = $this.attr('href');
		var style_menu = $('link[title*="menu"]');
			style_menu.attr('href', mPath);	
		return false;
	});
	
	// Set Global Color
	jQuery('a.styleswitch_global').bind('click',function() {
		var $this = $(this);
		var mPath = $this.attr('href');
		var style_menu = $('link[href*="styles_"]');
			style_menu.attr('href', mPath);	
		return false;
	});
	
	jQuery('#panel .stCols span').click(function(){
		var bgCol = jQuery(this).css('background-color');
		jQuery('#panel .stCols span').removeClass('current');
		jQuery(this).addClass('current');
		jQuery('#panel #bgColor').css({backgroundColor:bgCol});
		jQuery('body').css({backgroundColor:bgCol});
	});
		
	
	// Set Samples Patterns
	jQuery('#panel .stPat a').click(function(){
		var BgAttr = jQuery(this).attr('href');
		jQuery('#panel .stPat a').removeClass('current');
		jQuery(this).addClass('current');
		jQuery('body').css({backgroundImage:'url(' + BgAttr + ')'});
		if (jQuery(this).hasClass('bg_t')){
			jQuery('body').css({backgroundRepeat:'repeat', backgroundPosition:'0 0', backgroundAttachment:'scroll'});
		} else {
			jQuery('body').css({backgroundPosition:'0 0', backgroundAttachment:'fixed'});
		}
		return false;
	});
	
	/*
	jQuery('#hFont option').each(function() {
		var thisHfont = jQuery(this).text();
		jQuery(this).css({fontFamily: thisHfont });
	});	  

	jQuery('#cFont option').each(function() {
			var thisCfont = jQuery(this).val();
		jQuery(this).css({fontFamily: thisCfont });	
    });
	
	
	 // Set Heading Font
	 jQuery("#hFont").change(onSelectChangeHeading);
	
	 function onSelectChangeHeading(){
		 var selected = jQuery("#hFont option:selected");  
		 var output = "";
		 if(selected.val() != 0){
		  output = selected.text();
		 }
		 jQuery('h1, h2, h3, h4, h5, h6, .logo a').css('font-family', output);
		}
		

	// Set Content Font
	jQuery("#cFont").change(onSelectChangeContent);
	
	function onSelectChangeContent(){
		 var selected = jQuery("#cFont option:selected"); 
		 var output = "";
		 if(selected.val() != 0){
		  output = selected.text();
		 }
		 jQuery('body, p').css('font-family', output);
		}
	*/
	
	// Set Samples Color Links
	jQuery('#panel #lColor').ColorPicker({
		onChange:function(hsb, hex, rgb){
			jQuery('#panel').find('#lColor').css({backgroundColor:'#' + hex});
			jQuery('.link').css({color:'#' + hex});
		},
		onSubmit:function(hsb, hex, rgb, el){
/*			jQuery('#panel .stCols span').removeClass('current');*/
			jQuery(el).find('#lColor').css({backgroundColor:'#' + hex});
			jQuery(el).ColorPickerHide();
		}
	});

	
	// Reset Panel
	jQuery('#styleReset').click(function(){
			window.location.reload();
			jQuery("a.styleswitch[rel='menu_dark']").trigger('click');
		return false;
	});

	
});